﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;

using CanDoo;
using CanDoo.Core;
using CanDoo.Data;
using CanDoo.Web;
using CanDoo.FineUI;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Admin.Core.Contracts;
using FineUIPro;

namespace CanDoo.Oil.WebApp.cusSupplierContacts
{
    public partial class Edit : EditPagebase<ISupplierContactsContract, SupplierContacts>
    {
        public ISupplierContract SupplierContract { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //FineUIListHelper.BindDDL<Supplier>(SupplierContract.GetResolvedTree(), 0L, ed_Supplier,true);
                //FineUIListHelper.BindDDL(SupplierContract.EntityName, this.SupplierContract.GetSelectList(), 0L, ed_Supplier,true);
                if (PageMode != pagemode.Add)
                    LoadData();
            }
        }

        protected override void LoadData(SupplierContacts entity)
        {
            ed_Name.SetValue(entity.Name);
            if (entity.Supplier!=null)
            {
                ed_Supplier.Values=new string[] { entity.Supplier.Id.ToString() };
                ed_Supplier.Text=entity.Supplier.Name;
            }
            ed_Sex.SetValue(entity.Sex);
            ed_Mobile.SetValue(entity.Mobile);
            ed_Tel.SetValue(entity.Tel);
            ed_Title.SetValue(entity.Title);
            ed_Fax.SetValue(entity.Fax);
            ed_Email.SetValue(entity.Email);
            ed_Zip.SetValue(entity.Zip);
            ed_Address.SetValue(entity.Address);
        }

        protected override OperationResult SaveData(SupplierContacts entity)
        {
            entity.Name = ed_Name.GetValue().ToString();
            if (ed_Supplier.Values.Count() > 0 && ed_Supplier.Values[0].ToLong() >  0L)
                entity.SupplierId = ed_Supplier.Values[0].ToLong();
            else
                entity.SupplierId = 0L;
            entity.Sex = ed_Sex.GetValue().ToInt();
            entity.Mobile = ed_Mobile.GetValue().ToString();
            entity.Tel = ed_Tel.GetValue().ToString();
            entity.Title = ed_Title.GetValue().ToString();
            entity.Fax = ed_Fax.GetValue().ToString();
            entity.Email = ed_Email.GetValue().ToString();
            entity.Zip = ed_Zip.GetValue().ToString();
            entity.Address = ed_Address.GetValue().ToString();
            return base.SaveData(entity);
            
        }
    }
}
